<?php
// contact to database
$connect = mysql_connect("localhost", "root", "") or die ("Error , check your server connection.");
mysql_select_db("stms");
 
//Get data in local variable
$Regno=$_POST['Regno'];
$Indexno=$_POST['Indexno'];
$SubjectCode=$_POST['SubjectCode'];
$Grade=$_POST['Grade'];



$result=mysql_query("SELECT * FROM results WHERE Regno='$Regno' and SubjectCode='$SubjectCode'");
 $insert="Add";
 $update="Update";
 $delete="Delete";
 
 if($_POST['ADD']==$insert)
 {
 
 if (mysql_num_rows($result) == 1){
 
 	 header("Location:http://localhost/stms/pages/Resultadmin.php?err=This Regno is already exist!");

 //echo "This Regno is already exist";
 }
 else
 {
// check for null values
	if ($Regno=="" or $SubjectCode=="")
	 header("Location:http://localhost/stms/pages/Resultadmin.php?err=Regno and SubjectCode must be entered, hit back button and re-enter information!");
	//echo "All fields must be entered, hit back button and re-enter information";
	else{
	$query="insert into results values('$Regno','$Indexno','$SubjectCode','$Grade')";
	mysql_query($query)  or die(mysql_error());
		 header("Location:http://localhost/stms/pages/Resultadmin.php?err=Your message has been received!");

	//echo "Your message has been received";
	}
	}
}
else if($_POST['UPDATE']==$update)
{
if (mysql_num_rows($result) == 1)
{
	$upd="UPDATE results SET Indexno='$Indexno',Grade='$Grade' WHERE Regno='$Regno' and SubjectCode='$SubjectCode'";

	mysql_query($upd)  or die(mysql_error());
	header("Location:http://localhost/stms/pages/Resultadmin.php?err=Updated successfully!");

	//echo "Updated successfully";
 }
 
 else header("Location:http://localhost/stms/pages/Resultadmin.php?err=There is no Regno as you given!");
 //echo "There is no Regno as you given";
 }
 else if($_POST['DELETE']==$delete)
 {
 
if (mysql_num_rows($result) == 1)
 {
 $del="DELETE FROM results WHERE Regno = '$Regno' and SubjectCode='$SubjectCode'";
 mysql_query($del)  or die(mysql_error());
 header("Location:http://localhost/stms/pages/Resultadmin.php?err=Deleted successfully!");
	//echo "Deleted successfully";
	}
	else
	header("Location:http://localhost/stms/pages/Resultadmin.php?err=Regno is not there!");
	//echo "Regno is not there";
	
 
 }
 if(isset($_POST['Search']))
 {
	header("Location:http://localhost/stms/pages/Resultadmin.php?err1=$Regno.$SubjectCode");
 }
 if(isset($_POST['Search2']))
 {
  $se=$_POST['Searcher'];
	header("Location:http://localhost/stms/pages/Resultadmin.php?err2=$se");
 }
 
?>